import 'package:auntserver/application.dart';
import 'package:dio/dio.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';

import 'aunt_net_help.dart';
import 'response_interceptors.dart';
import 'net_constant.dart';

///请求头
Map<String, dynamic> optHeader = {
  'accept-language': 'zh-cn',
  'content-type': 'application/json'
};

var dio = new Dio(BaseOptions(connectTimeout: 30000, headers: optHeader));
///网络框架单例模式
class NetManager{
  static NetManager _instance = NetManager._internal();
  Dio _dio;

  static const CODE_SUCCESS = 200;
  static const CODE_TIME_OUT = -1;

  factory NetManager() => _instance;

  ///通用全局单例，第一次使用时初始化
  NetManager._internal({String baseUrl}) {
    if (null == _dio) {
      _dio = new Dio(
          new BaseOptions(baseUrl: baseUrl, connectTimeout: 15000));
         _dio.interceptors.add(new ResponseInterceptors());
    }
  }

  static NetManager getInstance({String baseUrl}) {
    if (baseUrl == null) {
      return _instance._normal();
    } else {
      return _instance._baseUrl(baseUrl);
    }
  }

  ///用于指定特定域名
  NetManager _baseUrl(String baseUrl) {
    if (_dio != null) {
      _dio.options.baseUrl = baseUrl;
    }
    return this;
  }

  ///一般请求，默认域名
  NetManager _normal() {
    if (_dio != null) {
      if (_dio.options.baseUrl != Api.getAuntUrl(AuntApp.appVersionStatus)) {
        _dio.options.baseUrl = Api.getAuntUrl(AuntApp.appVersionStatus);
      }
    }
    return this;
  }

  ///通用的GET请求
  get(api, {params, withLoading = true}) async {
    Response response;
    try {
      response = await _dio.get(api, queryParameters: params);
    } on DioError catch (e) {
      return resultError(e);
    }

    if (response.data is DioError) {
      return resultError(response.data['c']);
    }

    return response.data;
  }

  ///通用的POST请求
  post(api, {params, withLoading = true}) async {
    Response response;
    if(withLoading){
      print("xlb--net loading...");
      EasyLoading.show(status: 'loading...');
    }
    try {
      print("-----------访问:"+api.toString()+"-----------");
      response = await _dio.post(api, queryParameters: params);
    } on DioError catch (e) {
      if(EasyLoading.isShow){
        print("xlb--net loading end...");
        EasyLoading.dismiss(animation: true);
      }
      return resultError(e);
    }

    if (response.data is DioError) {
      if(EasyLoading.isShow){
        print("xlb--net loading end...");
        EasyLoading.dismiss(animation: true);
      }
      return resultError(response.data['c']);
    }
    if(EasyLoading.isShow){
      print("xlb--net loading end...");
      EasyLoading.dismiss(animation: true);
    }
    return response.data;
  }
}

ResultData resultError(DioError e) {
  Response errorResponse;
  if (e.response != null) {
    errorResponse = e.response;
  } else {
    errorResponse = new Response(statusCode: 666);
  }
  if (e.type == DioErrorType.CONNECT_TIMEOUT ||
      e.type == DioErrorType.RECEIVE_TIMEOUT) {
    errorResponse.statusCode = Code.NETWORK_TIMEOUT;
  }

  return new ResultData(
      data:errorResponse.statusMessage, isSuccess:false, statusCode:errorResponse.statusCode);
}

